<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.roleName" placeholder="角色名称" clearable style="width: 200px;" class="filter-item" />
      <el-button v-waves class="filter-item light-warning-btn" type="warning" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <div style="display:inline-block;" v-if="isPrivilege('jzx-role-save')">
        <router-link :to="{path:`/role/add`}">
          <el-button v-waves class="filter-item light-primary-btn" style="margin-left: 10px;" type="primary" icon="el-icon-plus">
            新增
          </el-button>
        </router-link>
      </div>
    </div>
    <div>
      <div class="contents_list">
        <el-table v-loading="listLoading" :data="rolesList" border fit highlight-current-row style="width: 100%">
          <el-table-column align="center" label="序号" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="角色编码" prop="roleCode" width="200"></el-table-column>
          <el-table-column align="center" label="角色名称" prop="roleName" width="200"></el-table-column>
          <el-table-column align="header-center" label="角色描述" min-width="200">
            <template slot-scope="scope">
              {{ scope.row.roleDesc }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" min-width="150" v-if="isPrivilege('jzx-role-del') || isPrivilege('jzx-role-update')">
            <template slot-scope="scope">
              <router-link :to="`/role/edit/`+scope.row.id">
                <el-button v-if="isPrivilege('jzx-role-update')" type="primary" size="mini">
                  修改
                </el-button>
              </router-link>
              <el-button v-if="isPrivilege('jzx-role-del')" style="margin-left: 10px" type="danger" size="mini" @click="handleDelete(scope)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>
    </div>
  </div>
</template>

<script>
import { getRolesPage, deleteRole } from '@/api/system/role'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import { getUserId } from '@/utils/auth'
import { isPrivilege } from '@/utils/constUtils'

const defaultRole = {
  key: '',
  name: '',
  description: '',
  routes: []
}

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: null,
      total: 0,
      role: Object.assign({}, defaultRole),
      routes: [],
      rolesList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      listLoading: true,
      listLoading1: true,
      authSystemId: null,
      data: { },
      listQuery: JSON.parse(localStorage.getItem('roleQuery')) || {
        pageIndex: 1,
        pageSize: 20,
        roleName: undefined,
        authSystemId: null,
        userId: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    isPrivilege(resourceCode) {
      return isPrivilege(resourceCode)
    },
    getList() {
      this.listLoading = true
      this.listQuery.userId = getUserId()
      getRolesPage(this.listQuery).then(response => {
        this.rolesList = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
      localStorage.setItem('roleQuery', JSON.stringify(this.listQuery))
    },
    handleFilter() {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    handleDelete({ $index, row }) {
      this.$confirm('确认删除角色?', '操作提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteRole({ id: row.id })
          this.rolesList.splice($index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>

<style scoped>
  .filter-container{background-color:#DCE5EA;padding:15px;}
  .contents_list{padding-top:10px;}
</style>
